
public class TamponNPlaces {
	
	private char[] T;
	private int nbElements;
	private int tete;
	private int capacite;
	
	public TamponNPlaces(int n){
		T = new char[n];
		nbElements = 0;
		tete = 0;
		capacite = n;
	}

	public synchronized void produire(char c){
		while(nbElements==capacite){  // attend tampon non plein
			try {wait();} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		T[(tete+nbElements) % capacite] = c;
		nbElements++;
		notifyAll();  // signale tampon non vide
	}
	
	public synchronized char consommer(){
		while(nbElements==0){   // attend tampon non vide
			try {wait();} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		char tmp = T[tete];
		tete = (tete+1) % capacite;
		nbElements--;
		notify();  // signale tampon non plein
		return tmp;
	}
}
